package com.own.component.util.time.base;

import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * DateTimeBaseUtil
 *
 * @author chenxueli
 * @date 2024-01-04 14:43:00
 */
public class DateTimeBaseUtil {

    private static final Map<String, DateTimeFormatter> FORMATTER_MAP = new HashMap<>();

    /**
     * 获取格式化对象信息
     *
     * @param pattern 格式化字符串
     * @return 格式化对象
     */
    public static DateTimeFormatter getDateTimeFormatter(String pattern) {
        if (!FORMATTER_MAP.containsKey(pattern)) {
            FORMATTER_MAP.put(pattern, DateTimeFormatter.ofPattern(pattern));
        }
        return FORMATTER_MAP.get(pattern);
    }

    /**
     * 构建格式化的字段串
     *
     * @param patterns 格式化的字段串
     * @return 格式化的字段串
     */
    public static String buildPattern(String... patterns) {
        if (patterns.length == 1) {
            return patterns[0];
        }
        var builder = new StringBuilder();
        for (var pattern : patterns) {
            builder.append("[").append(pattern).append("]");
        }
        return builder.toString();
    }

}
